//
//  twoViewController.m
//  diantai
//
//  Created by 吕斌利 on 2019/8/25.
//  Copyright © 2019 吕斌利. All rights reserved.
//

#import "twoViewController.h"
#import "model.h"
#import "FYVideoPlayVC.h"
#import <NSObject+YYModel.h>
@interface twoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
@property (nonatomic, strong) UITableView *tableView; ///<
@property (nonatomic, copy) NSArray *data; ///<数据源
@property (nonatomic, strong) model * mo;
@end

@implementation twoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    //    self.data = [NSMutableArray arrayWithArray:@[@"",@""]];
    
    [self request];
}
- (void)request {
    NSString *urlStr = @"http://fm.gerenhao.com/api/open/voice/union/select/1.3.9.json";
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    //创建request
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.HTTPMethod = @"POST";
    request.HTTPBody = [@"level=3&pageIndex=1&parentId=73&rootId=1" dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPShouldUsePipelining = YES;
    //创建NSURLSession
    NSURLSession *session = [NSURLSession sharedSession];
    //sessio÷÷
    //创建任务
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSDictionary *jsonInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
        
        self.data =  [NSArray modelArrayWithClass:[model class] json:[jsonInfo objectForKey:@"result"]];
        dispatch_async(dispatch_get_main_queue(), ^{
            // UI更新代码
            [self.tableView reloadData];
        });
        //      [self.tableView reloadData];
    }];
    
    //开始任务
    [task resume];
}
#pragma mark - UITableViewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        
    }
    model *mode=self.data[indexPath.row];
    cell.textLabel.text=mode.name;;
    //    cell.imageView.image=UIImageWithName(@"geli");
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return AdaptedWidth(70);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.data.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.delegetBlock) {
        self.delegetBlock(self.data, indexPath.row);
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - UI

#pragma mark - Setter & Getter

- (UITableView *)tableView {
    if (_tableView) {
        return _tableView;
    }
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,  kScreenWidth, kScreenHeight-kNavBarHeight-50)style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    _tableView.rowHeight = AdaptedHeight(105);
    _tableView.estimatedRowHeight = 0;
    _tableView.estimatedSectionHeaderHeight = 0;
    _tableView.estimatedSectionFooterHeight = 0;
    _tableView.backgroundColor=UIColorFromHex(0xF2F2F2);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    _tableView.sectionFooterHeight = 0;
    _tableView.sectionHeaderHeight = AdaptedWidth(5);
    return _tableView;
}

#pragma mark - JXPagingViewListViewDelegate

- (UIScrollView *)listScrollView {
    return self.tableView;
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}

- (UIView *)listView {
    return self.view;
}


@end
